<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\Traits\AdminTrait;
use App\Models\Admin\AdminCdjRole;
use App\Models\Admin\AdminOperateLogs;
use App\Models\Admin\AdminUser;
use App\Models\AdminRole;
use App\User;
use Auth,Hash,DB;
use Illuminate\Validation\Rule;

class UserController extends AdminController
{
    use AdminTrait;

    protected $model;
    protected $unique_id = '';


    /**
     * 定义验证规则
     */
    protected $validate_rule = [
        'role_id' => 'integer|min:1',
        'name' => 'required',
        'password' => 'nullable|min:6',
        'mobile' => 'regex:/^1[3456789][0-9]{9}$/',
        'email' => 'nullable|email',
    ];

    /**
     * 定特殊的验证规则
     */
    protected function rules(){
        return [
            'username' => [
                'required',
                Rule::unique('admin_user')->ignore($this->unique_id)
            ]
        ];
    }

    /**
     * 定义验证错误信息
     */
    protected $err_msg = [
        'role_id.min' => '请选择角色',
        'name.required' => '姓名不能为空',
        'username.required' => '用户名不能为空',
        'username.unique' => '用户名已存在',
        'password.min' => '密码长度需大于等于6位',
        'mobile.regex' => '手机格式不正确',
        'email.email' => '邮箱格式不正确',
    ];


    /**
     * 注入模型
     */
    public function __construct(AdminUser $model) {
        parent::__construct();
        $this->model = $model;
    }


    protected function search($request)
    {

        $select = $this->model;

        //关键字
        if ($request->keywords){
            $select = $select->where(function ($query)use ($request){
                $query->where('name', 'like', "%$request->keywords%")
                    ->orWhere('username', 'like', "%$request->keywords%")
                    ->orWhere('mobile', 'like', "%$request->keywords%");
            });
        }
        //角色
        (!empty($request->role_id)) && $select = $select->where('role_id',$request->role_id);

        return $select;
    }

    /**
     * 列表前置
     * @return []
     */
    protected function beforeIndex(){
        //角色列表
        $role = AdminCdjRole::pluck('name','id');
        return ['role'=>$role];
    }

    /**
     * 处理添加、编辑时的字段
     * @param $type false=add,true=edit
     * @return []
     */
    protected function setRequestField($arr,$type=false){
        if($type){
            if(!empty($arr['password'])){
                $arr['password'] = Hash::make($arr['password']);
            }else{
                unset($arr['password']);
            }
        }else{
            //处理密码
            isset($arr['password']) && $arr['password'] = Hash::make($arr['password']);
            //默认添加SAAS用户ID
        }

        //过滤字段
        if(isset($arr['_token'])) unset($arr['_token']);
        if (isset($arr['sign'])) unset($arr['sign']);
        if (isset($arr['timeStamp'])) unset($arr['timeStamp']);

        return $arr;
    }

    protected function checkDelete($id){
        $is_log=AdminOperateLogs::where('user_id',$id)->count();
        if($is_log){
            return ['errcode'=>'40011','errmsg'=>"此管理员已有相关日志，不能删除"];
        }
        $user_info=AdminUser::find($id);
        $is_last=AdminUser::where('role_id',1)->count();
        if($user_info->role_id==1 && $is_last==1)
        {
            return ['errcode'=>'-1','errmsg'=>"至少保留一个超级管理员"];
        }
        return true;
    }
}
